package cn.xunjie.ymcc.service.impl;

import cn.xunjie.ymcc.domain.Course;
import cn.xunjie.ymcc.domain.CourseMarket;
import cn.xunjie.ymcc.domain.CourseUserLearn;
import cn.xunjie.ymcc.domain.MediaFile;
import cn.xunjie.ymcc.dto.PayNotifyDto;
import cn.xunjie.ymcc.mapper.CourseUserLearnMapper;
import cn.xunjie.ymcc.service.ICourseMarketService;
import cn.xunjie.ymcc.service.ICourseService;
import cn.xunjie.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue.jiao
 * @since 2022-07-21
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {
    @Autowired
    private ICourseService courseService;
    @Autowired
    private ICourseMarketService courseMarketService;

    @Override
    public CourseUserLearn byIds(MediaFile mediaFile) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",3L).eq("course_id",mediaFile.getCourseId());
        CourseUserLearn courseUserLearn = selectOne(wrapper);
        return courseUserLearn;
    }

    @Override
    public void saveUserLear(PayNotifyDto payNotifyDto) {
        String extParams = payNotifyDto.getExtParams();
        Map<String,Object> map = JSONObject.parseObject(extParams, Map.class);
        Integer userId = (Integer) map.get("userId");
        String[] courseIds = map.get("courseId").toString().split(",");
        for (String courseId : courseIds) {
            Course course = courseService.selectById(courseId);
            CourseMarket market = courseMarketService.selectById(courseId);
            //查询是否已经有记录了
            CourseUserLearn learnTmp = selectByCourseIdAndLoginId(Long.valueOf(courseId), Long.valueOf(userId));
            Date date = new Date();
            if(learnTmp == null){
                //有就新增
                CourseUserLearn courseUserLearn = new CourseUserLearn();
                courseUserLearn.setLoginId(Long.valueOf(userId));
                courseUserLearn.setStartTime(date);
                courseUserLearn.setEndTime(DateUtils.addDays(date,market.getValidDays()));
                courseUserLearn.setState(CourseUserLearn.STATE_PAY);
                courseUserLearn.setCourseId(Long.valueOf(courseId));
                courseUserLearn.setCourseOrderNo(payNotifyDto.getOrderNo());
                courseUserLearn.setCreateTime(date);
                insert(courseUserLearn);
            }else {
                learnTmp.setCourseOrderNo(payNotifyDto.getOrderNo());
                //过期时间在现在时间之前说明过期了
                if(learnTmp.getEndTime().before(date)){
                    learnTmp.setLoginId(Long.valueOf(userId));
                    learnTmp.setStartTime(date);
                    learnTmp.setEndTime(DateUtils.addDays(date,market.getValidDays()));
                    learnTmp.setState(CourseUserLearn.STATE_PAY);
                    learnTmp.setCourseId(Long.valueOf(courseId));
                    learnTmp.setCourseOrderNo(payNotifyDto.getOrderNo());
                    learnTmp.setCreateTime(date);
                }else {
                    //还没过期 直接追加可看天数
                    Date endTime = DateUtils.addDays(learnTmp.getEndTime(), market.getValidDays());
                    learnTmp.setEndTime(endTime);
                }
                updateById(learnTmp);
            }



        }

    }

    public CourseUserLearn selectByCourseIdAndLoginId(Long courseId,Long loginId){
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId)
                .eq("login_id",loginId);
        return selectOne(wrapper);
    }
}
